package com.intel.factorytest.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.intel.factorytest.R;

import android.widget.Button;
import android.view.View;

public class UsbTest extends BaseActivity {//implements OnClickListener {
  private final String KEY_STATUS="item_test_status";
   
  private String TAG = "UsbTest";
  private TextView mcTestText;
  private Button mSucBtn;
  private static final String TEST_ITEM = "test_item";
  private boolean mAvailable = false; 
  private static final String TEST_ITEM_RESULT = "test_item_result";
  boolean is_pass = false;

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    String orientationStr = intent.getStringExtra("sd_orientation");
    int orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    Log.i(TAG,"orientationStr = " + orientationStr);
    if("SCREEN_ORIENTATION_PORTRAIT".equals(orientationStr)) {
        orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        super.setRequestedOrientation(orientation);

    } else if("SCREEN_ORIENTATION_LANDSCAPE".equals(orientationStr)) {
        orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        super.setRequestedOrientation(orientation);

    }
    setContentView(R.layout.usb_test);
    mcTestText = findViewById(R.id.usb_text);
    // mcTestText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    mcTestText.setGravity(Gravity.CENTER);
    mcTestText.setTextSize(24);
    // setContentView(mcTestText);
    mcTestText.setText(this.getString(R.string.insert_usb_cable));
    super.initView();
    mSucBtn = (Button) findViewById(R.id.success_button);
    mSucBtn.setVisibility(View.INVISIBLE);
    }


    @Override
    public void onResume() {
         super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction(UsbManager.ACTION_USB_STATE);
        registerReceiver(mIntentReceiver, filter);
    }



    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(UsbManager.ACTION_USB_STATE)) {
                mAvailable = intent.getBooleanExtra(UsbManager.USB_CONNECTED,
                        false);
            }
            if (mAvailable) {
                mcTestText.setText(getString(R.string.usb_status_device) + "\n"
                        + getString(R.string.usb_ok));
                is_pass = true;
                mSucBtn.setVisibility(View.VISIBLE);
            } else {
                mcTestText.setText(getString(R.string.insert_usb_cable));
                is_pass = false;
                mSucBtn.setVisibility(View.INVISIBLE);
            }
        }
    };


    @Override
    public void onPause() {
     super.onPause();
     unregisterReceiver(mIntentReceiver);
    }
  

    // private void setTestResult () {
    //     Intent intent = new Intent();
    //     Intent mIntent = getIntent();
    //     int pos = mIntent.getIntExtra(TEST_ITEM,-1);
    //     intent.putExtra(TEST_ITEM,pos);
    //     intent.putExtra(TEST_ITEM_RESULT,is_pass);
    //     setResult(0, intent);
    // }
    @Override
    public void finish() {
        setTestResult (); 
        super.finish();
    }
}
